<?php
/* SVN FILE: $Id: resell.php 222 2009-09-16 20:07:22Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Resell Items
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 222 $
* @lastrevision $Date: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_public/inventory/resell.php $
*/

class public_ibmarket_inventory_resell extends marketPublicCommand
{
	public function doExecute( ipsRegistry $registry )
	{
		if($this->memberData['member_id'] == 0 || !$this->memberData['g_market_use'])
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$inventoryItems = marketRegistry::getInventoryItem($this->memberData['member_id']);
		
		if(!is_array($inventoryItems[$this->request['item']]))
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$response = marketRegistry::getItem($this->caches['market_items'][$inventoryItems[$this->request['item']]['item']])->publicOnResoldItem($inventoryItems[$this->request['item']]);
		
		if(!$response && !is_null($response))
		{
			$this->registry->getClass('output')->showError('cannot_resell_item');
		}
		
		if($this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['resell_putback'] && numbers::compare($this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['stock']) >= 0)
		{
			$this->DB->update('market_items',array(
													'stock' => numbers::add($this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['stock'], 1),
			),"item_id=".$inventoryItems[$this->request['item']]['item']);
			
			$this->cache->rebuildCache('market_items','ibmarket');
		}
		
		$this->DB->insert('market_buy_logs',array(
													'member_id' => $this->memberData['member_id'],
													'method' => 'resell',
													'item_id' => $inventoryItems[$this->request['item']]['item'],
													'ip_address' => $this->member->ip_address,
													'time_bought' => IPS_UNIX_TIME_NOW,
													'quanity' => 1,
													'price_bought' => $inventoryItems[$this->request['item']]['price_bought'],
													'resell_price' => $inventoryItems[$this->request['item']]['resell_price'],
													'tax_total' => 0,
													'category' => $this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['category'],
													'item_name' => $this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['item_name'],
													'item_description' => marketRegistry::getItem($this->caches['market_items'][$inventoryItems[$this->request['item']]['item']])->parseInventoryDescription($inventoryItems[$this->request['item']]),
													'icon' => $this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['icon'],
													'category_name' => $this->caches['market_category'][$this->caches['market_items'][$inventoryItems[$this->request['item']]['item']]['category']]['cat_name'],
				)						);
		
		IPSMember::save( $this->memberData['member_id'], array( 'core' => array( $this->settings['market_members_field'] => numbers::add($this->memberData[$this->settings['market_members_field']],$inventoryItems[$this->request['item']]['resell_price']) ) ) );
		
		$this->DB->delete('market_inventory',"i_id=".$this->request['item']);
		
		$this->registry->getClass('output')->redirectScreen( $this->lang->words['resold_item'], $this->settings['base_url_with_app'] . "module=inventory" );
	}
}